﻿using System.Collections.Generic;
using Newtonsoft.Json;
using WoogiWorld;
using WoogiWorld.Event;

namespace GMS
{
public class SystemPlatform : SingletonBehaviour<SystemPlatform>
{
    public Platform platform;
    public WebGLPlatform webGLPlatform = WebGLPlatform.unknown;

    private void Awake()
    {
        EventDispatcher.Instance.AddEventListener("gms-woogi-event-webgl.Platform", this, OnWebGLPlatform);
    }

    private void OnDestroy()
    {
        EventDispatcher.Instance.RemoveEventListener("gms-woogi-event-webgl.Platform", this);
    }

    public void Start()
    {
        SetPlatform();
        
  
        
#if UNITY_WEBGL
    #if UNITY_EDITOR
            webGLPlatform = WebGLPlatform.win;
    #else
                // SystemInfo.operatingSystem
               Dictionary<string, object> dic = new Dictionary<string, object>();
                dic.Add("cmd", "checkPlatform");
                string json = JsonConvert.SerializeObject(dic);
                EventDispatcher.Instance.DispatchEvent("Gms.UnityPushInfo", json);
    #endif
#endif
    }

    private void OnWebGLPlatform(EventMessage info)
    {
        if (info.CheckData())
        {
            string platformstr = (string)info.Data;
            switch (platformstr)
            {
                case "ipad":
                case "iphone":
                    webGLPlatform = WebGLPlatform.ios;
                    break;
                case "win":
                    webGLPlatform = WebGLPlatform.win;
                    break;
                case "mac":
                    webGLPlatform = WebGLPlatform.mac;
                    break;
                case "android":
                    webGLPlatform = WebGLPlatform.and;
                    break;
                case "chrome_book":
                    webGLPlatform = WebGLPlatform.chromebook;
                    break;
                default:
                    webGLPlatform = WebGLPlatform.unknown;
                    break;
            }
        }
    }

    void SetPlatform()
    {
#if UNITY_WEBGL
        platform = Platform.webgl;
#else
    #if UNITY_STANDALONE_WIN
               platform = Platform.win;
    #elif UNITY_STANDALONE_OSX
            platform = Platform.mac;
    #elif UNITY_IPHONE
            platform = Platform.ios;
    #elif UNITY_ANDROID
        #if GOOGLE_PLAY
                     platform = Platform.googlePlay;
        #elif HUA_WEI
                    platform = Platform.android_huawei;
        #elif XIAO_MI
                    platform = Platform.android_xiaomi;
        #else
                platform = Platform.and;
        #endif
    #elif UNITY_WEBGL
            platform = Platform.webgl;
    #endif
#endif
    }
}
}